package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Contractor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ContractorDao {

    /**
     * 通过承包商cid查找唯一承包商
     * @param cid
     * @return
     */
    @Select("select * from contractor where cid = #{cid} and status = 1")
    Contractor findConByCid(String cid);


    /**
     * 找到所有承包商信息
     * @return
     */
    @Select("select * from contractor where status = 1")
    List<Contractor> findContractors();

    /**
     * 分页查询所有承包商
     * @param queryPageBean
     * @return
     */
    @Select({"<script> SELECT * FROM contractor <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and cname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Contractor> findContractorsByPage(QueryPageBean queryPageBean);


    /**
     * 符合分页查询的总条数
     * @param queryString
     * @return
     */
    @Select({"<script> SELECT count(*) FROM contractor <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and cname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);


    /**
     * 逻辑删除，执行更新操作，将属性status的值修改为0，表示将该数据删除
     * @param cid
     * @return
     */
    @Update("UPDATE contractor set status = 0 where cid = #{cid}")
    int updateStatusContractor(@Param("cid")String cid);


    /**
     * 新增一个开发商
     * @param contractor
     * @return
     */
    @Insert("insert into contractor(cname,cid,updatetime,status)" +
            " values(#{cname},#{cid},#{updatetime},1)")
    int saveContractor(Contractor contractor);
}
